$(function () {
    var item = GetQueryString("item");
    var sort = GetQueryString("sort");
    //加载类型列表 
    var typeSel = $.Params.create();
    typeSel.ajax("get", getAdminIP() + "dictionarys/" + sort, function (data) {
        if (data.success) {
            var typeHtml = "<option value = '0'>请选择分类</option>";
            $.each(data.result, function (i, val) {
                typeHtml += "<option value = '" + val.codeId + "'>" + val.codeDesc + "</option>";
            })
            $("#typeList").html(typeHtml);
        } else {
            alert(data.msg);
        }
    })
    //查询列表页
    var sel = $.Params.create();
    sel.addParams("page", 0);
    sel.addParams("rows", rows);
    sel.addParams("item", item);
    getData();
    //搜索查询
    $("#searchBtn").on("click", function () {
        var type = $("#typeList").val();
        var pTitle = $("#searchVal").val();
        if (type != "0") {
            sel.addParams("type", type);
        } else {
            sel.addParams("type", undefined);
        }
        if (pTitle != "") {
            sel.addParams("pTitle", pTitle);
        } else {
            sel.addParams("pTitle", undefined);
        }
        getData();
    })

    //删除记录
    $("#delBtn").on("click", function () {
        var params = getCheckData();
        if (params != "") {
            if (confirm("确定要删除吗！")) {
                var del = $.Params.create();
                del.ajax("delete", getAdminIP() + "videos?params=" + params, function (data) {
                    if (data.success) {
                        alert(data.msg);
                        getData();
                    } else {
                        alert(data.msg);
                    }
                })
            }
        } else {
            alert("请选择要删除的记录！");
        }
    })

    //添加按钮
    $("#addDataBtn").on("click", function () {
        window.location.href = "metAdd.html?item=" + item + "&sort=" + sort;
    })



    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })
    //列表页
    function getData() {
        getPageData("get", sel, getAdminIP() + "videos", function (data) {
            if (data.success) {
                var videoHtml = "";
                if (data.total > 0) {
                    $.each(data.result, function (i, val) {
                        var videoImg = getIP() + "data/videoCover/" + val.videoImg;
                        var type = "";
                        if (val.dictionary != null) {
                            type = val.dictionary.codeDesc;
                        }
                        videoHtml += "<tr role='row' class='odd'>" +
                            "<td><input type='checkbox' eid='" + val.id + "' onclick='getCheck(this)' class='classOption'></td>" +
                            "<td><img src='" + videoImg + "' style='width:100%' /></td >" +
                            "<td>" + val.expertName + "</td >" +
                            "<td>" + val.expertHospital + "</td>" +
                            "<td>" + val.title + "</td>" +
                            "<td>" + type + "</td>" +
                            "<td>" + val.createTime + "</td>" +
                            "<td><button type='button' onClick='selComment(\"" + val.id + "\",\"" + item + "\",\"" + sort + "\")' class='btn btn-primary'>查看评论</button><button type='button' onClick='update(\"" + val.id + "\",\"" + item + "\",\"" + sort + "\")' class='btn btn-primary'>修改</button><button type='button' onClick = 'setTop(\"" + val.id + "\")' class='btn btn-primary'>置顶</button></td>" +
                            "</tr>";
                    })
                } else {
                    videoHtml = "<tr><td colspan='8' class='txCenter'>暂无数据</td></tr>";
                }
                $("#listContainer").html(videoHtml);
            } else {
                alert(data.msg);
            }
        })
    }

    //置顶
    setTop = function (eid) {
        var topSel = $.Params.create();
        topSel.ajax("put", getAdminIP() + "videos/" + eid, function (data) {
            if (data.success) {
                getData();
            } else {
                alert(data.msg);
            }
        })
    }
})

//修改
function update(eid, item, sort) {
    window.location.href = "metUpdate.html?eid=" + eid + "&item=" + item + "&sort=" + sort;
}

//查看评论
function selComment(eid, item, sort) {
    window.location.href = "metPlList.html?titleId=" + eid + "&item=" + item + "&sort=" + sort;
}